{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 4 ways to print in GAlgebra"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sympy import *\n",
    "from galgebra.ga import Ga\n",
    "from galgebra.printer import Eprint, Format, xpdf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "cga3d = Ga(r'e_1 e_2 e_3 e e_{0}',g='1 0 0 0 0,0 1 0 0 0,0 0 1 0 0,0 0 0 0 -1,0 0 0 -1 0')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Printing in plain text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "e_1^e_2^e_3^e^e_{0}\n"
     ]
    }
   ],
   "source": [
    "print(cga3d.I())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##  Enhanced Console Printing\n",
    "\n",
    "If called `Eprint()` upfront, `print()` will do Enhanced Console Printing(colored printing with ANSI escape sequences):\n",
    "\n",
    "- code: https://github.com/pygae/galgebra/blob/master/examples/Terminal/terminal_check.py\n",
    "- output: https://nbviewer.jupyter.org/github/pygae/galgebra/blob/master/examples/ipython/Terminal.ipynb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[0;34me_1\u001b[0m^\u001b[0;34me_2\u001b[0m^\u001b[0;34me_3\u001b[0m^\u001b[0;34me\u001b[0m^\u001b[0;34me_0\u001b[0m\r\n"
     ]
    }
   ],
   "source": [
    "!python -c \"from galgebra.ga import Ga;from galgebra.printer import Eprint;Eprint();cga3d = Ga(r'e_1 e_2 e_3 e e_{0}',g='1 0 0 0 0,0 1 0 0 0,0 0 1 0 0,0 0 0 0 -1,0 0 0 -1 0');print(cga3d.I())\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## MathJax printing in Jupyter Notebook\n",
    "\n",
    "In a Jupyter Notebook upfront, without calling `print()`, it will do LaTeX printing with MathJax for Geometric Algebra expressions.\n",
    "\n",
    "- code & output: https://nbviewer.jupyter.org/github/pygae/galgebra/blob/master/examples/ipython/colored_christoffel_symbols.ipynb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*}  \\boldsymbol{e}_{1}\\wedge \\boldsymbol{e}_{2}\\wedge \\boldsymbol{e}_{3}\\wedge \\boldsymbol{e}\\wedge \\boldsymbol{e}_{{0}} \\end{equation*}"
      ],
      "text/plain": [
       "e_1^e_2^e_3^e^e_{0}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cga3d.I()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## LaTeX printing\n",
    "\n",
    "If called `Format()` upfront and `xpdf()` eventually, `print()` will do LaTeX printing (internally, the standard output will be redirected to a buffer for later consumption).\n",
    "\n",
    "- code: https://github.com/pygae/galgebra/blob/master/examples/LaTeX/colored_christoffel_symbols.py\n",
    "- output: https://nbviewer.jupyter.org/github/pygae/galgebra/blob/master/examples/ipython/LaTeX.ipynb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "Format()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(cga3d.I())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "xpdf(filename='test_latex_output.tex',paper=(9,10),pdfprog=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\r\n",
      "\\documentclass[10pt,fleqn]{report}\r\n",
      "\\usepackage[vcentering]{geometry}\r\n",
      "\\geometry{papersize={9in,10in},total={8in,9in}}\r\n",
      "\r\n",
      "\\pagestyle{empty}\r\n",
      "\\usepackage[latin1]{inputenc}\r\n",
      "\\usepackage{amsmath}\r\n",
      "\\usepackage{amsfonts}\r\n",
      "\\usepackage{amssymb}\r\n",
      "\\usepackage{amsbsy}\r\n",
      "\\usepackage{tensor}\r\n",
      "\\usepackage{listings}\r\n",
      "\\usepackage{color}\r\n",
      "\\usepackage{xcolor}\r\n",
      "\\usepackage{bm}\r\n",
      "\\usepackage{breqn}\r\n",
      "\\definecolor{gray}{rgb}{0.95,0.95,0.95}\r\n",
      "\\setlength{\\parindent}{0pt}\r\n",
      "\\DeclareMathOperator{\\Tr}{Tr}\r\n",
      "\\DeclareMathOperator{\\Adj}{Adj}\r\n",
      "\\newcommand{\\bfrac}[2]{\\displaystyle\\frac{#1}{#2}}\r\n",
      "\\newcommand{\\lp}{\\left (}\r\n",
      "\\newcommand{\\rp}{\\right )}\r\n",
      "\\newcommand{\\paren}[1]{\\lp {#1} \\rp}\r\n",
      "\\newcommand{\\half}{\\frac{1}{2}}\r\n",
      "\\newcommand{\\llt}{\\left <}\r\n",
      "\\newcommand{\\rgt}{\\right >}\r\n",
      "\\newcommand{\\abs}[1]{\\left |{#1}\\right | }\r\n",
      "\\newcommand{\\pdiff}[2]{\\bfrac{\\partial {#1}}{\\partial {#2}}}\r\n",
      "\\newcommand{\\lbrc}{\\left \\{}\r\n",
      "\\newcommand{\\rbrc}{\\right \\}}\r\n",
      "\\newcommand{\\W}{\\wedge}\r\n",
      "\\newcommand{\\prm}[1]{{#1}'}\r\n",
      "\\newcommand{\\ddt}[1]{\\bfrac{d{#1}}{dt}}\r\n",
      "\\newcommand{\\R}{\\dagger}\r\n",
      "\\newcommand{\\deriv}[3]{\\bfrac{d^{#3}#1}{d{#2}^{#3}}}\r\n",
      "\\newcommand{\\grade}[1]{\\left < {#1} \\right >}\r\n",
      "\\newcommand{\\f}[2]{{#1}\\lp{#2}\\rp}\r\n",
      "\\newcommand{\\eval}[2]{\\left . {#1} \\right |_{#2}}\r\n",
      "\\newcommand{\\Nabla}{\\boldsymbol{\\nabla}}\r\n",
      "\\newcommand{\\eb}{\\boldsymbol{e}}\r\n",
      "\\usepackage{float}\r\n",
      "\\floatstyle{plain} % optionally change the style of the new float\r\n",
      "\\newfloat{Code}{H}{myc}\r\n",
      "\\lstloadlanguages{Python}\r\n",
      "\r\n",
      "\\begin{document}\r\n",
      "\\begin{equation*} \\boldsymbol{e}_{1}\\wedge \\boldsymbol{e}_{2}\\wedge \\boldsymbol{e}_{3}\\wedge \\boldsymbol{e}\\wedge \\boldsymbol{e}_{{0}} \\end{equation*}\r\n",
      "\\end{document}\r\n"
     ]
    }
   ],
   "source": [
    "!cat test_latex_output.tex"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
